//logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    logs: [],
    inputValue:"",
    penddingCount:2,
    allSelect:false,
    list:[
      {text:"吴彦祖分祖编写的小程序",checked:false},
      {text:"今天也要开心鸭！",checked:true},
      {text:"羞答答的玫瑰静悄悄的开",checked:false},
    ],
  },
  getInputValue(e){
    // console.log(e.detail.value)
    this.setData({
      inputValue:e.detail.value
    })
  },
  addItem(){
    // console.log(this.data.inputValue)
    var list = this.data.list
    list.push({
      text:this.data.inputValue,
      checked:false
    })
    // this.data.inputValue = ""
    this.setData({
      list:list,
      inputValue:"",
      penddingCount:this.data.penddingCount + 1
    })
  },
  // 改变选中状态
  changeStatus(e){
    // console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    var list = this.data.list
    list[index].checked = !list[index].checked
    this.setData({
      list:list,
      penddingCount:this.data.penddingCount + (list[index].checked?-1:1)
    })
  },
  // 删除当前todo项
  deleteItem(e){
    var index = e.currentTarget.dataset.index
    var countlist = this.data.list[index]
    this.data.list.splice(index,1)
    console.log(index)
    // console.log(this.data.list)
    this.setData({
      list:this.data.list,
      penddingCount:this.data.penddingCount - (countlist.checked?0:1)
    })
  },
  // 全选
  allChecked(){
    this.data.allSelect = !this.data.allSelect
    this.data.list.forEach((item)=>{
      item.checked = this.data.allSelect
    })
    this.setData({
      allSelect:this.data.allSelect,
      list:this.data.list,
      penddingCount:this.data.allSelect ? 0 : this.data.list.length
    })
  },
  // 选中删除
  deleteChecked(){
    var newTodo = this.data.list.filter((item)=>{
      return !item.checked
    })
    this.setData({
      list:newTodo
    })
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  }
})
